package start.demo01.fileIO;

import java.io.*;
import java.util.zip.*;
import java.util.zip.ZipEntry;

public class FileZip {

    public static Boolean zip(String inputFileName, String zipFileName) throws Exception {
        zip(zipFileName, new File(inputFileName));
        return true;
    }
    private static void zip(String zipFileName, File inputFile) throws Exception {
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
        zip(out, inputFile, "");
        out.flush();
        out.close();
    }
    private static void zip(ZipOutputStream out, File f, String base) throws Exception {
        if (f.isDirectory()) {
            File[] fl = f.listFiles();
            out.putNextEntry(new ZipEntry(base + "/"));
            base = base.length() == 0 ? "" : base + "/";
            for (int i = 0; i < fl.length; i++) {
                zip(out, fl[i], base + fl[i].getName());
            }
        } else {
            out.putNextEntry(new ZipEntry(base));
            FileInputStream in = new FileInputStream(f);
            int b;
            while ((b = in.read()) != -1) {
                out.write(b);
            }
            in.close();
        }
    }

    public static void main(String [] temp){
        try {
//            zip("C:\\Users\\wangcj\\Desktop\\tmp\\img0", "C:\\Users\\wangcj\\Desktop\\tmp\\img.zip");//你要压缩的文件夹      和  压缩后的文件
            ZipUncompress("C:\\Users\\wangcj\\Desktop\\tmp\\img.zip", "C:\\Users\\wangcj\\Desktop\\tmp\\img");
        }catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * zip解压
     * @param inputFile   待解压文件名
     * @param destDirPath 解压路径
     */

    public static void ZipUncompress(String inputFile, String destDirPath) throws Exception {
        File srcFile = new File(inputFile);//获取当前压缩文件
        // 判断源文件是否存在
        if (!srcFile.exists()) {
            throw new Exception(srcFile.getPath() + "所指文件不存在");
        }
        //开始解压
        //构建解压输入流
        ZipInputStream zIn = new ZipInputStream(new FileInputStream(srcFile));
        ZipEntry entry = null;
        File file = null;
        while ((entry = zIn.getNextEntry()) != null) {
            if (!entry.isDirectory()) {
                file = new File(destDirPath, entry.getName());
                if (!file.exists()) {
                    new File(file.getParent()).mkdirs();//创建此文件的上级目录
                }
                OutputStream out = new FileOutputStream(file);
                BufferedOutputStream bos = new BufferedOutputStream(out);
                int len = -1;
                byte[] buf = new byte[1024];
                while ((len = zIn.read(buf)) != -1) {
                    bos.write(buf, 0, len);
                }
                // 关流顺序，先打开的后关闭
                bos.close();
                out.close();
            }
        }
    }
//
//
//    public static void main(String[] args) {
//        try {
//            ZipCompress("C:\\Users\\wangcj\\Desktop\\tmp\\img0", "C:\\Users\\wangcj\\Desktop\\tmp\\img.zip");
////            ZipUncompress("C:\\Users\\wangcj\\Desktop\\tmp\\img.zip", "C:\\Users\\wangcj\\Desktop\\tmp\\img");
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
}
